package net.dfrz.oa.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;

import net.dfrz.oa.action.support.BaseAction;
import net.dfrz.oa.dto.WorkLogDto;
import net.dfrz.oa.pojo.User;
import net.dfrz.oa.pojo.WorkLog;
import net.dfrz.oa.service.WorkLogService;
import net.dfrz.oa.utils.PageBean;
import net.dfrz.oa.viewvo.WorkLogVo;

@Controller
@Scope("prototype")
@Namespace("")
@Results({
		@Result(name="workLog",location="worklog.jsp"),
		@Result(name="workLogList", type="json", params={"root","jsonMap"})
})
public class WorkLogAction extends BaseAction<WorkLog>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private WorkLog workLog;
	
	private WorkLogVo workLogVo;
	
	private WorkLogDto workLogDto;
	@Autowired
	private WorkLogService workLogService;
	//分页
	private PageBean<WorkLog> pageList = new PageBean<WorkLog>();
	Map<String, Object> jsonMap = new HashMap<String, Object>();//定义map
	private String rows;//每页显示的记录数 
    private String page;//当前第几页 
    private List<WorkLog> workLogList = null; //存放当前页数据
    private List<WorkLogVo> workLogVoList = new ArrayList<WorkLogVo>();
    //end
	@Override
	public WorkLog getModel() {
		workLog = new WorkLog();
		return workLog;
	}
	
	public Map<String, Object> getJsonMap() {
		return jsonMap;
	}

	public void setJsonMap(Map<String, Object> jsonMap) {
		this.jsonMap = jsonMap;
	}

	public String getRows() {
		return rows;
	}

	public void setRows(String rows) {
		this.rows = rows;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public List<WorkLog> getWorkLogList() {
		return workLogList;
	}

	public void setWorkLogList(List<WorkLog> workLogList) {
		this.workLogList = workLogList;
	}

	public WorkLogDto getWorkLogDto() {
		return workLogDto;
	}

	public void setWorkLogDto(WorkLogDto workLogDto) {
		this.workLogDto = workLogDto;
	}

	/**
	 * 个人工作日志初始界面
	 */
	@Action("worklog_worklogManage")
	public String worklog(){
		return "workLog";
	}
	
	/**
	 * 查询工作日志
	 * @throws Exception 
	 */
	@Action("worklog_queryPageList")
	public String queryPageList() throws Exception{
		//当前页   
        int intPage = Integer.parseInt((page == null || page == "0") ? "1":page);  
        //每页显示条数  
        int number = Integer.parseInt((rows == null || rows == "0") ? "10":rows);  
        pageList.setPageNo(intPage);
        pageList.setPageSize(number);
        //springdata分页结果
        Page<WorkLog> page = null;
        
        //是否为条件查询判断  ---录入时间和userId
        String queryType = (String) getRequest().getParameter("type");
        //获取当前用户的id
        Integer uid = ((User)getSession().getAttribute("currentUser")).getId();
        if("date".equals(queryType)){ //带时间的条件查询
        	//-----------------进来
        	String query = (String) getRequest().getParameter("date");
        	//将String---date
        	Date date = stringToDate(query, "yyyy-MM-dd");
        	
        	page = workLogService.queryPageList(pageList, date,uid);	
        }else{
        	page = workLogService.queryPageList(pageList,uid);
        }
        workLogList = page.getContent();
        Long total = page.getTotalElements();
        for(WorkLog po : workLogList){
        	Integer id = po.getId();
        	String title = po.getTitle();
        	String log = po.getLog();
        	Date date = po.getDate();
        	String d = dateToString(date, "yyyy-MM-dd");
        	Integer userId = uid;
        	workLogVo = new WorkLogVo(id,userId,title,log,d);
        	workLogVoList.add(workLogVo);
        }
        jsonMap.put("total", total);
        jsonMap.put("rows", workLogVoList);
		return "workLogList";
	}

	/**
	 * 添加用户日志
	 */
	@Action("worklog_addWorklog")
	public void addWorklog() throws Exception{
		User user = (User) getSession().getAttribute("currentUser");
		String title = workLogDto.getTitle();
		String log = workLogDto.getLog();
		String pageDate = workLogDto.getDate();
		//将String date ->Date date
		Date date = stringToDate(pageDate, "yyyy-MM-dd");
		WorkLog worklog = new WorkLog(); 
		worklog.setTitle(title);
		worklog.setLog(log);
		worklog.setUser(user);
		worklog.setDate(date);
		workLogService.addWorkLog(worklog);
		getResponse().getWriter().write("1");
	}
	
	/**
	 * 修改用户日志
	 * @throws Exception 
	 */
	@Action("worklog_updateWorklog")
	public void updateWorklog() throws Exception{
		Integer worklogId = workLogDto.getWorklogId();
		String title = workLogDto.getTitle();
		String log = workLogDto.getLog();
		String pageDate = workLogDto.getDate();	
		//将String date ->Date date
		Date date = stringToDate(pageDate, "yyyy-MM-dd");
		//System.out.println(worklogId+":"+title+":"+log+":"+pageDate);
		//执行更新
		workLogService.updateWorkLog(worklogId,title,log,date);
		getResponse().getWriter().write("true");
	}
}
